/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import com.openbravo.bean.Attribute;
import com.openbravo.bean.AttributeValue;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.SerializerReadInteger;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

/**
 *
 * @author Administrator
 */
public class AttributeValueDAO extends ModifiedLogDAO<AttributeValue>{

    public AttributeValueDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                    "ATTRIBUTEVALUE",
                    new Field[]{
                       new Field("ID",Datas.STRING, Formats.STRING),
                       new Field("ATTRIBUTE_ID",Datas.STRING, Formats.STRING,Attribute.class),
                       new Field("VALUE",Datas.STRING, Formats.STRING),
                       new Field("LASTMODIFIED",Datas.TIMESTAMP, Formats.TIMESTAMP)
                     },
                    new int[]{0}
                    );
    }

    @Override
    public void writeInsertValues(DataWrite dp, AttributeValue obj) throws BasicException {
          dp.setString(1, obj.getId());
          dp.setString(2,obj.getAttributeID());
          dp.setString(3,obj.getValue());
          dp.setTimestamp(4,obj.getLastModified());
    }

    @Override
    public Class getSuportClass() {
       return AttributeValue.class;
    }

    public AttributeValue readValues(DataRead dr,AttributeValue obj) throws BasicException {
       if(obj==null)
    	   obj=new AttributeValue();
       obj.setId(dr.getString(1));
       obj.setAttributeID(dr.getString(2));
       obj.setValue(dr.getString(3));
       obj.setLastModified(dr.getTimestamp(4));
       return obj;
    }

    public List<AttributeValue> listAttributeValueByProduct(String productId)throws BasicException{
    	return new StaticSentence(s,this.getTable().getListSQL("A")+",ATTRIBUTEUSE B,PRODUCTS D WHERE A.ATTRIBUTE_ID=B.ATTRIBUTE_ID AND B.ATTRIBUTESET_ID=D.ATTRIBUTESET_ID  AND D.ATTRIBUTESET_ID IS NOT NULL AND D.ID=?",
                SerializerWriteString.INSTANCE,
                this).list(productId);
    }
    
}
